import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import br.com.vm.model.Mapa;
import br.com.vm.model.Rota;
import br.com.vm.model.vo.MelhorRotaReq;

import com.google.gson.Gson;

public class Main {

	public static void main(String[] args) throws Exception {
		URL url = new URL(
				"http://localhost:8888/vm-web/mapa/json/obterMelhorRota");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type", "application/json");

		MelhorRotaReq melhorRotaReq = new MelhorRotaReq();
		melhorRotaReq.setNomeDoMapa("MS");
		melhorRotaReq.setPontoDeOrigem("2222");
		melhorRotaReq.setPontoDeDestino("444");
		melhorRotaReq.setAutonomiaDoCaminhao(10.00);
		melhorRotaReq.setValorDoLitroDoCombustivel(2.50);

		Gson gson = new Gson();

		String objJSON = gson.toJson(melhorRotaReq);

		OutputStream os = conn.getOutputStream();
		os.write(objJSON.getBytes());
		os.flush();

		try {
			conn.getResponseCode();

			if (conn.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
				imprimirMensagem(conn.getErrorStream());
			} else {
				imprimirMensagem(conn.getInputStream());
			}

			conn.disconnect();

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

	}

	public static void main2(String[] args) throws Exception {
		URL url = new URL("http://localhost:8888/vm-web/mapa/json/salvarMapa");
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setDoOutput(true);
		conn.setRequestMethod("POST");
		conn.setRequestProperty("Content-Type", "application/json");

		Mapa mapa = new Mapa();
		mapa.setNome("MS");
		mapa.setRotas(new ArrayList<Rota>());
		mapa.getRotas().add(new Rota("111", "222", 1.0));
		mapa.getRotas().add(new Rota("111", "555", 1.0));
		
		mapa.getRotas().add(new Rota("222", "111", 2.0));
		mapa.getRotas().add(new Rota("222", "555", 1.0));
		mapa.getRotas().add(new Rota("222", "333", 1.0));
		
		mapa.getRotas().add(new Rota("333", "666", 1.0));
		mapa.getRotas().add(new Rota("333", "888", 1.0));
		
		mapa.getRotas().add(new Rota("444", "888", 1.0));
		
		mapa.getRotas().add(new Rota("555", "111", 2.0));
		mapa.getRotas().add(new Rota("555", "666", 1.0));
		
		mapa.getRotas().add(new Rota("666", "333", 2.0));
		mapa.getRotas().add(new Rota("666", "777", 1.0));
		
		mapa.getRotas().add(new Rota("777", "444", 2.0));
		mapa.getRotas().add(new Rota("777", "888", 1.0));


		Gson gson = new Gson();

		String objJSON = gson.toJson(mapa);

		OutputStream os = conn.getOutputStream();
		os.write(objJSON.getBytes());
		os.flush();

		try {
			conn.getResponseCode();

			if (conn.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
				imprimirMensagem(conn.getErrorStream());
			} else {
				imprimirMensagem(conn.getInputStream());
			}

			conn.disconnect();

		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

	}

	private static void imprimirMensagem(InputStream poIs) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader((poIs)));

		String output;
		System.out.println("Output from Server .... \n");
		while ((output = br.readLine()) != null) {
			System.out.println(output);
		}
	}

}
